define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wu/bomproductspec/index',
                    add_url: 'wu/bomproductspec/add',
                    edit_url: 'wu/bomproductspec/edit',
                    del_url: 'wu/bomproductspec/del',
                    multi_url: 'wu/bomproductspec/multi',
                    table: 'wu_bomproductspec',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        //{field: 'id', title: __('Id')},
                        //{field: 'productspec_id', title: __('Productspec_id')},
                        //{field: 'bomtype_id', title: __('Bomtype_id')},
                        {field: 'productspec.name', title: __('Productspec.name'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        //{field: 'bomtype.productnumber', title: __('Bomtype.productnumber'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        //{field: 'bomtype.productname', title: __('Bomtype.productname'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'name', title: __('原材料'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'num', title: __('Num'), operate:false},
                        {field: 'kcnum', title: __('庫存數量'), operate:false},
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'productspec.id', title: __('Productspec.id')},
                        //{field: 'productspec.spectype_id', title: __('Productspec.spectype_id')},
                        //{field: 'productspec.specnumber', title: __('Productspec.specnumber')},
                        //{field: 'productspec.nbznumber', title: __('Productspec.nbznumber')},
                        //{field: 'productspec.lycl', title: __('Productspec.lycl')},
                        //{field: 'productspec.create_time', title: __('Productspec.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'productspec.update_time', title: __('Productspec.update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'bomtype.id', title: __('Bomtype.id')},
                        //{field: 'bomtype.create_time', title: __('Bomtype.create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                    var myColumns=[];
                    $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                         myColumns.push(item.field);
                    });
                    $.cookie('fa_wu_bomproductspec', JSON.stringify(myColumns), { expires: 365, path: '/'});
             });
             if($.cookie('fa_wu_bomproductspec')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_wu_bomproductspec')), function(i, item) {
                  table.bootstrapTable('hideColumn', item);
                });
             }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});